package net.zretc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.zretc.entitys.Pet;
import net.zretc.service.PetService;
import net.zretc.service.PetServiceImpl;

/**
 * Servlet implementation class ShangServlet
 */
@WebServlet("/Shang.do")
public class ShangServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private PetService petService=new PetServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShangServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String a=request.getParameter("id");
		int id=0;
		if(a!=null) {
			id=Integer.parseInt(a);
			System.out.println();
			ServletContext application=this.getServletConfig().getServletContext();
			Object b=application.getAttribute("shelveslist");
			List<Pet> list1=petService.findAll();
			Pet pet=null;
			if(b!=null) {
				List<Pet> list=(List<Pet>)b;
				for(int i=0;i<list.size();i++) {
					if(list.get(i).getId()==id) {
						pet=list.get(i);
					}
				}
			}
			System.out.println("pet:"+pet);
			String d=pet.getTitle();
			for(int i=0;i<list1.size();i++) {
				if(list1.get(i).getTitle().equals(d)) {
					pet=null;
					break;
				}
			}
			System.out.println("pet1:"+pet);
			if(pet!=null)
			petService.insert1(pet);
			response.sendRedirect("houtaishang.do");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
